package fanshe4;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class TestUser {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<?> userClass = Class.forName("fanshe4.User");
        Object o = userClass.newInstance();
        System.out.println("o = " + o);

        Constructor<?> constructor = userClass.getConstructor(String.class);//通过构造器实现传参赋值
      Object instance = constructor.newInstance("想果然");
        System.out.println("instance = " + instance);

        Constructor<?> declaredConstructor = userClass.getDeclaredConstructor(String.class, Integer.class);
        Object instance1 = declaredConstructor.newInstance("昂达", 90);
        System.out.println("instance1 = " + instance1);

        System.out.println("-------------------");
        Field[] fields = userClass.getFields();//获取属性名
        for (Field field : fields) {
            System.out.println(field.getName());
        }

        System.out.println("------------------------");
        Field[] fields1 = userClass.getDeclaredFields();
        for (Field field : fields1) {
            System.out.println(field.getName());
        }
    }

}
